<?php $this->pageHeader('Vaktdetaljer'); ?>

<?php echo $this->flashMessage()->renderMessages(); ?>

<?php
	$isLeaderOnShift = false;
	foreach ($this->shift_roles as $role)
	{
		if (!is_null($role->Member) && $role->Member->id === $this->whoami->member_id &&
			(bool)$role->leader === true)
		{
			$isLeaderOnShift = true;
			break;
		}
	}
    $isAdmin = $this->acl()->isAllowed('service:shift', 'edit');
    $isAdminOrLeader = $isAdmin || $isLeaderOnShift;

    if($isAdminOrLeader) :
?>
    <?php if ($isAdmin) : ?>
            <a href="<?php echo $this->url(array('module' => 'admin', 'controller' => 'shift', 'action' => 'notify-all', 'id' => $this->shift->id), null, true); ?>" class="button">
                <img src="<?php echo $this->imageLink('images/icons/shift_notifyall.png'); ?>" alt="Send vaktutkalling" title="Send vaktutkalling" />
                Send vaktutkalling
            </a>
        <?php if (!is_null($this->shift->shift_report_id)) : ?>
            <a href="<?php echo $this->url(array('module' => 'admin', 'controller' => 'shift', 'action' => 'report', 'id' => $this->shift->id), null, true); ?>" class="button">
                <img src="<?php echo $this->imageLink('images/icons/shift_add.png'); ?>" alt="Vis vaktrapport" title="Vis vaktrapport" />
                Vis rapport
            </a>
        <?php endif; ?>
    <?php endif; ?>
    <?php if ($isAdminOrLeader && ($this->shift->ShiftReport == null || ($this->shift->ShiftReport !== null && !(bool)$this->shift->ShiftReport->approved))) : ?>
        <?php $name = $this->shift->ShiftReport !== null && !(bool)$this->shift->ShiftReport->approved ? 'Endre vaktrapport' : 'Lag vaktrapport'; ?>
            <a href="<?php echo $this->url(array('module' => 'core', 'controller' => 'shift', 'action' => 'report', 'id' => $this->shift->id), null, true); ?>" class="button">
                <img src="<?php echo $this->imageLink('images/icons/shift_add.png'); ?>" alt="<?php echo $name; ?>" title="<?php echo $name; ?>" />
                <?php echo $name; ?>
            </a>
    <?php endif; ?>
<?php endif; ?>

<p class="clear"></p>

<p id="top_right_links">
<a href="<?php echo $this->url(array('module' => 'core', 'controller' => 'event', 'action' => 'view', 'id' => $this->shift['event_id'])); ?>" class="button"><img src="<?php echo $this->imageLink('images/icons/arrow_right.png'); ?>" />Gå til arrangement</a>
    <?php if ($this->isAdmin) : ?>
        <a href="<?php echo $this->url(array('module' => 'admin', 'controller' => 'shift', 'action' => 'edit', 'id' => $this->shift['id']), null, true); ?>" class="button"><img src="<?php echo $this->imageLink('images/icons/pencil.png'); ?>" />Rediger</a>
    <?php endif; ?>
</p>

<h2>Detaljer for "<?php echo $this->escape($this->shift->name); ?>"</h2>

<?php echo $this->render('shift/_shift.phtml'); ?>

<p>&nbsp;</p>

<h2>Mannskap på denne vakten</h2>
<table class="admin_member_shift_personell">
    <thead>
        <tr>
            <th>Rolle</th>
            <th>Navn</th>
        <?php if($isAdminOrLeader) : ?>
            <th>Utkalling sendt</th>
            <th>Utkalling bekreftet</th>
        <?php endif; ?>
            <th>&nbsp;</th>
        </tr>
    </thead>
    <tbody>
        <?php foreach($this->shift_roles as $role) : ?>
            <?php $member = $role->Member !== null ? $role->Member : false; ?>
            <tr>
                <td><?php echo $this->escape($role->Role->name); ?></td>
                <td id="cell-<?php echo $role['id']; ?>"><?php echo $member ? $this->escape($member->first_name . ' ' . $member->last_name) : ''; ?></td>

            <?php if($isAdminOrLeader) : ?>
                <td>
                <?php
                    $notificationSent = '-';
                    if (!is_null($role->notification_sent_time)) {
                        $notificationSent = new RedCross_Date();
                        $notificationSent->import($role->notification_sent_time);
                        $notificationSent = $notificationSent->getDateTimeStr();
                    }
                    echo $notificationSent;
                ?>
                </td>
                <td>
                <?php
                    $notificationConfirmed = '-';
                    if (!is_null($role->notification_confirmed_time)) {
                        $notificationConfirmed = new RedCross_Date();
                        $notificationConfirmed->import($role->notification_confirmed_time);
                        $notificationConfirmed = $notificationConfirmed->getDateTimeStr();
                    }
                    echo $notificationConfirmed;
                ?>
                </td>
            <?php endif; ?>
            
                <td>
                    <?php if($isAdminOrLeader && $member) : ?>
                        <a href="<?php echo $this->url(array('module' => 'admin', 'controller' => 'shift', 'action' => 'notify', 'id' => $role->id), null, true); ?>"><img src="<?php echo $this->imageLink('/images/icons/shift_notify.png'); ?>" alt="Send utkalling" title="Send utkalling" /></a>
                    <?php endif; ?>
                    <?php if($member && $role->notification_sent_time && !$role->notification_confirmed_time && (($isAdminOrLeader && !(bool)$role->leader) || $role->Member->id === $this->whoami->member_id)) : ?>
                        <a href="<?php echo $this->url(array('module' => 'core', 'controller' => 'shift', 'action' => 'confirm', 'id' => $role->id, 'key' => RedCross_Global::shift_role_hash($role)), null, true); ?>"><img src="<?php echo $this->imageLink('/images/icons/shift_confirm.png'); ?>" alt="Bekreft" title="Bekreft rolle" /></a>
                    <?php endif; ?>
                    <?php if($isLeaderOnShift && !$role['leader'] && $member) : ?>
                        <a id="member_role_link-<?php echo $role['id']; ?>" class="leader_change_role" href="<?php echo $this->url(array('module' => 'core', 'controller' => 'shift', 'action' => 'replace', 'role' => $role['id']), null, true); ?>"><img src="<?php echo $this->imageLink('images/icons/member_edit.png'); ?>" alt="Endre Person" title="Endre Person" /></a>
                    <?php endif; ?>
                </td>
            </tr>
        <?php endforeach; ?>
    </tbody>
</table>
